﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AscendEast_DataEntry
{
    public partial class Form_Centre_Delete : Form
    {
        DataAccess dal = new DataAccess();
        public Form_Centre_Delete()
        {
            InitializeComponent();
            PopulateDropDown_centres();
        }

        private void PopulateDropDown_centres()
        {
            string select_centres_query = "select * from tbl_centres";
            DataTable dt0 = dal.Select(select_centres_query);
            DataTable dt = new DataTable();
            dt.Columns.Add("centre_code", typeof(String));
            dt.Columns.Add("centre_name", typeof(String));
            //dt.Columns.Add("state_code", typeof(String));
            dt.Rows.Add("null", "Select Centre");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                dt.Rows.Add(dt0.Rows[i][0], dt0.Rows[i][1]);
            }

            cb_centre_name_delete_search.DisplayMember = dt.Columns["centre_name"].ToString();
            cb_centre_name_delete_search.ValueMember = dt.Columns["centre_code"].ToString();
            cb_centre_name_delete_search.DataSource = dt;
        }

        private void btn_search_delete_centre_Click(object sender, EventArgs e)
        {
            if (cb_centre_name_delete_search.SelectedIndex == 0)
                MessageBox.Show("Please select any Centre");
            else
            {
                string centre_code = cb_centre_name_delete_search.SelectedValue.ToString();                
                PopulateDataGridView_Centre(centre_code);
            }
        }

        private void PopulateDataGridView_Centre(string centre_code)
        {
            string edit_centre_query = "select * from tbl_centres where centre_code = '" + centre_code + "'";

            dgv_centre_delete.DataSource = null;
            dgv_centre_delete.Rows.Clear();
            dgv_centre_delete.Refresh();

            DataTable dt = new DataTable();
            dt = dal.Select(edit_centre_query);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgv_centre_delete.Rows.Add(dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2]);
            }
        }

        private void btn_delete_centre_Click(object sender, EventArgs e)
        {
            if (cb_centre_name_delete_search.SelectedIndex == 0)
                MessageBox.Show("Please select a Centre");
            else
            {
                if (MessageBox.Show("Do you want to delete?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string centre_code = cb_centre_name_delete_search.SelectedValue.ToString();
                    String delete_centre_query = "delete from tbl_centres where centre_code = '" + centre_code + "'";
                    bool deleted_flag = dal.Delete(delete_centre_query);

                    if (deleted_flag == true)
                        MessageBox.Show("Record Deleted Successfully");
                    else
                        MessageBox.Show("Try again");

                    dgv_centre_delete.DataSource = null;
                    dgv_centre_delete.Rows.Clear();
                    dgv_centre_delete.Refresh();
                    
                    centre_code = null;

                    PopulateDropDown_centres();
                }
            }
        }

        private void btn_cancel_centre_delete_Click(object sender, EventArgs e)
        {
            dgv_centre_delete.DataSource = null;
            dgv_centre_delete.Rows.Clear();
            dgv_centre_delete.Refresh();
            cb_centre_name_delete_search.SelectedIndex = 0; 
        }
    }
}
